using System;
using System.Collections.Generic;
using System.Linq;
using Book_Shop.DAL.DAO.CustomException;

namespace Book_Shop.DAL.DAO
{
    public class OrderSheet
    {
        public string Status { get; set; }
        public List<Book> Books { get; private set; }
        public Shop Shop { get; private set; }
        public Reader Reader { get; private set; }
        public string OrderLocation { get; set; }
        public SalesPerson SalesPerson { get; set; }
        public double TotalPrice
        {
            get { return Books.Sum(book => book.TotalPrice); }
        }

        public DateTime DateOfOrder { get; set; }

        public int Id { get; set; }

        public OrderSheet()
        {
            Books = new List<Book>();
            Shop = new Shop();
            Reader = new Reader();
            SalesPerson = new SalesPerson();
        }

        public void Add(Book book)
        {
            bool IssameBook = false;

            foreach (Book aBook in Books)
            {
                if (aBook.Id==book.Id)
                {
                    IssameBook = true;
                    aBook.Qty += book.Qty;
                    break;

                }
            }
            if (!IssameBook)
            {
                Books.Add(book);
            }
            
        }

        public void Remove(Book book)
        {
            Books.Remove(book);
        }

        public void Add(ShoppingCart cart)
        {
            if (cart.Books.Count==0)
            {
                throw new EmptyCartException();
            }
           Books.AddRange(cart.Books);
        }

        public void Add(Shop shop)
        {
            Shop = shop;
        }

        public void Add(Reader reader)
        {
            Reader = reader;
        }
    }
}